<template>
  <div v-if="nodeIds.length !== 0">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="元素名称" prop="name">
          <el-input v-model.trim="queryParams.name" placeholder="请输入元素名称搜索" clearable/>
        </el-form-item>
        <el-form-item label="定位表达式" prop="expression">
          <el-input v-model.trim="queryParams.expression" placeholder="请输入元素定位搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-form-item label="定位方式" prop="locationType">
          <el-select v-model="queryParams.locationType" placeholder="请选择定位方式">
            <el-option v-for="item in bys" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-button type="primary" icon="el-icon-plus" @click="openDialog('新建元素')" v-permission="'element:add'">新建元素</el-button>
    </div>
    <div class="create-btn">
      <el-table :data="tableData" element-loading-spinner="el-icon-loading" element-loading-text="加载中。。。"
                element-loading-custom-class="equipment-loading" element-loading-background="rgba(55,55,55,0.2)"
                height="calc(100vh - 419px)">
        <el-table-column fixed prop="num" label="序号" width="100"></el-table-column>
        <el-table-column prop="name" label="元素名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="locationType" label="定位类型" width="100"></el-table-column>

        <el-table-column prop="expression" label="定位表达式" show-overflow-tooltip>
          <template v-slot:default="scope">
            <span @click="copy(scope.row.expression)" style="cursor: pointer">{{scope.row.expression}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="创建人" width="100"></el-table-column>
<!--        <el-table-column prop="createTime" label="创建时间" width="150"></el-table-column>-->
        <el-table-column prop="updateTime" label="更新时间" width="150"></el-table-column>
        <el-table-column label="操作" width="90px">
          <template v-slot:default="scope">
            <el-button type="text" @click="openDialog('编辑元素',scope.row)" v-permission="'element:update'">编辑</el-button>
            <el-popconfirm
              @onConfirm="handleDelete(scope.$index, scope.row)"
              confirm-button-text='确定'
              cancel-button-text='我再看看'
              icon="el-icon-info"
              icon-color="red"
              title="是否确认删除？"
            >
              <el-button slot="reference" type="text" v-permission="'element:delete'">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        @pagination="getElementList"
      />
    </div>
    <cu-dialog :dialogVisble="visible" :title="title" :editData="editData" @closeDialog="closeDialog"
               @refreshList="restList"></cu-dialog>
  </div>
</template>

<script>

import Pagination from "@/components/Pagination/index";
import CuDialog from "./cuDialog";


export default {
  name: "elementLibrary",
  props: ['nodeIds'],
  components: {Pagination,CuDialog},
  data() {
    return {
      queryParams: {
        name: "",
        locationType: "",
        createUser: "",
        expression: ""
      },
      tableData: [],
      total: 0,
      query: {
        pageRow: 10,
        pageNum: 1,
        projectId: sessionStorage.getItem("projectId")
      },
      visible: false,
      title: "",
      editData: {},
      bys: [
        {label: "xpath", value: "xpath"},
        {label: "id", value: "id"},
        {label: "name", value: "name"},
        {label: "tagName", value: "tagName"},
        {label: "className", value: "className"},
        {label: "cssSelector", value: "cssSelector"},
        {label: "linkText", value: "linkText"},
        {label: "partialLinkText", value: "partialLinkText"},
        {label: "byTitle", value: "byTitle"},
        {label: "byValue", value: "byValue"},
        {label: "withText", value: "withText"},
        {label: "text", value: "text"},
      ],
    }
  },
  watch: {
    nodeIds() {
      const loading = this.$loading({
        lock: true, // lock的修改符--默认是false
        text: 'Loading', // 显示在加载图标下方的加载文案
        spinner: 'el-icon-loading', // 自定义加载图标类名
        background: 'rgba(0, 0, 0, 0.1)', // 遮罩层颜色
        target: document.querySelector('#table')//loading覆盖的dom元素节点
      })
      this.getElementList()
      loading.close()
    }
  },
  methods: {
    copy(data){
      //创建input标签
      let input = document.createElement('input')
      //将input的值设置为需要复制的内容
      input.value = data;
      //添加input标签
      document.body.appendChild(input)
      //选中input标签
      input.select()
      //执行复制
      document.execCommand('copy')
      //成功提示信息
      this.$message.success('复制成功')
      //移除input标签
      document.body.removeChild(input)
    },
    getElementList() {
      this.query = {...this.query, ...this.queryParams}
      this.query["moduleId"] = this.nodeIds
      this.api({
        url: "/getElementList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.getElementList();
    },
    restList() {
      this.query.pageNum = 1;
      this.getElementList();
      this.closeDialog()
    },
    handleEdit(data) {
      console.log(data)
    },
    handleDelete(index,data) {
      this.api({url: `/deleteElement/${data.id}`, method: "delete"}).then((resp)=>{
        this.getElementList()
        this.$message({message: resp, type: 'success'});
      })
    },
    closeDialog() {
      this.visible = false;
    },
    openDialog(title, data = null) {
      this.title = title;
      this.visible = true;
      this.editData = data ? data : {}
    }
  }
}
</script>

<style scoped>
.search {
  margin: auto;
  text-align: center;
}

.create-btn {
  margin-top: 10px;
}
</style>
